package com.mifan.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mifan.common.Results;
import com.mifan.entity.User;
import com.mifan.mapper.UserMapper;
import com.mifan.utils.JwtTokenUtil;
import com.mifan.vo.ResultData;
import com.mifan.vo.UserListVo;
import com.mifan.vo.UserPageVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Milan
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserMapper userMapper;

    @Resource
    PasswordEncoder passwordEncoder;


    /**
     * 分页查询用户列表
     * @param userPageVo
     * @return
     */
//    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("/list")
    public Results list(UserPageVo userPageVo) {
        PageHelper.startPage(userPageVo.getPageNum(), userPageVo.getPageSize());
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        wrapper.ne("id", 1);
        List<UserListVo> users = userMapper.getUsersWithRoles();
        PageInfo page = new PageInfo(users);
        ResultData resultData = ResultData.createResultData(page);
        return Results.success(resultData);
    }


    /**
     * 获取用户详情
     * @return
     */
    @GetMapping("/info")
    public Results info(HttpServletRequest request) {
        String token = request.getHeader("Token");
        String username = JwtTokenUtil.extractUser(token);
        UserListVo user = userMapper.getUserByUsername(username);
        return Results.success(user);
    }


    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @PostMapping("/update")
    public Results update(@RequestBody User user) {
        int updateById = userMapper.updateById(user);
        System.out.println(user);
        return Results.success(null);
    }


    /**
     * TODO 删除用户
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Results delete(@RequestParam Long id) {
        int result = userMapper.deleteById(id);
        if (result > 0) {
            return Results.success(null);
        }
        return Results.failure("删除失败");
    }


    /**
     * TODO 新增用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    public Results add(@RequestBody User user) {
        user.setPassword(passwordEncoder.encode("123456"));
        int result = userMapper.insert(user);
        if (result > 0) {
            return Results.success(null);
        }
        return Results.failure("新增失败");
    }


    /**
     * TODO 修改密码
     * @param id 用户id
     * @param password 新密码
     */
    @GetMapping("/updatePassword")
    public Results updatePassword(@RequestParam Long id, @RequestParam String password) {
        User user = new User();
        user.setId(id);
        user.setPassword(passwordEncoder.encode(password));
        int result = userMapper.updateById(user);
        if (result > 0) {
            return Results.success(null);
        }
        return Results.failure("修改失败");
    }

}
